package org.tools.xml;

import java.io.ByteArrayOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.tools.constants.StringPool;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/** 
 * w3c xml对象操作类
 *
 * <pre> 
 * 构建组：e-tools
 * 作者：eddy
 * 邮箱：1546077710@qq.com
 * 日期：2017年4月23日-下午6:19:00
 * 版权：E源时代
 * </pre>
 */
public class W3CXmlUtil {
	
	/**
	 * 根据Element获取这个对应的元素的xml。
	 * @param element
	 * @return 
	 * String
	 * @exception 
	 * @since  1.0.0
	 */
	public static String getXML(Element element) {
		TransformerFactory transFactory = TransformerFactory.newInstance();
		try {
			Transformer transformer = transFactory.newTransformer();
			transformer.setOutputProperty("encoding", StringPool.UTF_8);
			transformer.setOutputProperty("indent", "yes");

			DOMSource source = new DOMSource();
			source.setNode(element);
			StreamResult result = new StreamResult();
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			result.setOutputStream(baos);
			transformer.transform(source, result);
			return baos.toString(StringPool.UTF_8);
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 在当前节点下，获取指定名称的子元素。
	 * 
	 * @param ruleNode
	 * @param name
	 * @return Element
	 * @exception
	 * @since 1.0.0
	 */
	public static Element getChildNodeByName(Element ruleNode, String name) {
		NodeList nodeList = ruleNode.getChildNodes();
		for (int i = 0; i < nodeList.getLength(); i++) {			
			Element node = getElement(nodeList.item(i));				
			if (node!=null && node.getTagName()!=null && node.getTagName().equalsIgnoreCase(name)) {
				return node;
			}			
		}
		return null;
	}
	
	private static Element getElement(Object object){
		if(object!=null && object instanceof Element){
			Element node = (Element) object;
			return node;
		}
		return null;
	}
	
	public static Document getDocument(String xmlFilePath){
		Document doc = null;
		try {			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();		
			doc = db.parse(xmlFilePath);					
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}
	
	public static NodeList selectNodes(String express, Object source) {// 查找节点，返回符合条件的节点集。
		NodeList result = null;
		XPathFactory xpathFactory = XPathFactory.newInstance();
		XPath xpath = xpathFactory.newXPath();
		try {
			result = (NodeList) xpath.evaluate(express, source,
					XPathConstants.NODESET);
		} catch (XPathExpressionException e) {
			e.printStackTrace();
		}

		return result;
	}

	public static Element selectFirstElement(String express,Object source){
		NodeList list = selectNodes(express, source);
		for(int i=0;i<list.getLength();i++){
			Object obj = list.item(i);
			if(obj instanceof Element){
				return (Element)obj;
			}
		}
		return null;
	}
}
